package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.User;
import com.example.service.UserService;
import com.example.service.UtilpageService;
import com.example.util.RespPageEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
//@CrossOrigin
public class UserController {
    @Resource
      private UserService userService;

   @Resource
   private UtilpageService utilpageService;
    @PostMapping("/dologin")
    public boolean dologin(@RequestBody User user){
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_nickname",user.getUserNickname());
        queryWrapper.eq("user_password",user.getUserPassword());
        User user1 =userService.getOne(queryWrapper);
        System.out.println("user1 = " + user1);
        if (user1 ==null){
            return false;
        }
        return true;
    }

    @GetMapping("/getlist")
    public List<User>getlist(){
        List<User>list =userService.list();
        return list;
    }
    @PostMapping("/add")

    public boolean add(@RequestBody User user){
        user.setUserTx( "http://localhost:8080/static/image/"+user.getUserTx());
        boolean user1 =userService.save(user);
        if (user1){

            return true;
        }
        return false;
    }

    @GetMapping("/del")
    public  boolean del(@RequestParam("userId") int userId){

        boolean user1 =userService.removeById(userId);
        if (user1){

            return true;
        }
        return false;
    }
    @GetMapping("/getOne")
    public User getOne(@RequestParam("userId") int userId){
        User user =userService.getById(userId);
        return  user;
    }

    @PostMapping("/upd")
    public  boolean upd(@RequestBody User user){
        user.setUserTx( "http://localhost:8080/static/image/"+user.getUserTx());
        boolean user1 =userService.updateById(user);
        if (user1){

            return true;
        }
        return false;
    }
//分页查询
    @GetMapping("/page")
    public RespPageEntity getAllUserByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        return utilpageService.getAllUserByPage(page,size);
    }
}
